import { Connection } from 'mongoose';
import { getInstance } from '../../db';
import { accountSchema } from '../../schemas/accountSchema';

interface IUpdate {
  uid?: string;
  username?: string;
  password?: string;
  phoneNumber?: string;
  [props: string]: any;
}

export async function updateUserInfoService(params: IUpdate):Promise<IAnyObject> {
  const db: Connection = await getInstance();

  const { uid }  = params;

  // 去掉uid
  delete params.uid;

  const accountmodel = db.model('accountmodel', accountSchema);

  const condition = {
    uid,
  };

  // 返回更新后的数据
  const result = await accountmodel.findOneAndUpdate(condition, params, { new: true });

  return result;
}


// 忘记密码，更新用户密码，这里没有uid，只能通过手机号来进行更新
export async function updateUserPasswordService(params: IUpdate):Promise<IAnyObject> {
  const db: Connection = await getInstance();

  const { phoneNumber }  = params;

  // 去掉uid
  delete params.phoneNumber;

  const accountmodel = db.model('accountmodel', accountSchema);

  const condition = {
    phoneNumber,
  };

  // 返回更新后的数据
  const result = await accountmodel.findOneAndUpdate(condition, params, { new: true });

  return result;
}
